<?

	include "db_connect.php";
	$db_connector = new db_connect;
	$db_connector -> db_connect();
	

	function get_usercode(){
		mysql_query("SET NAMES utf8");
		$sql = "SELECT * FROM user";
		$user_result = mysql_query($sql);
		for($i=1; $i<= mysql_num_rows($user_result); $i++){
			$user_row = mysql_fetch_row($user_result);
			$user_data[$i] = $user_row[0];
			
		}
		
		return $user_data;
	}
	
	function get_category(){
		mysql_query("SET NAMES utf8");
		$sql = "SELECT * FROM category";
		$category_result = mysql_query($sql);
		for($i=1; $i<= mysql_num_rows($category_result); $i++){
			$category_row = mysql_fetch_row($category_result);
			$category_data[$i] = $category_row[1];
		}
		return $category_data;
	}

	function get_interest_product(){
		mysql_query("SET NAMES utf8");
		$sql = "SELECT `increment`, `user_id`, `interest_product` FROM interest_product where $user_id LIMIT 5";
		$interest_result = mysql_query($sql);
		
		for($i=1; $i<= mysql_num_rows($interest_result); $i++){
			$interest_row = mysql_fetch_row($interest_result);
			$interest_result[$i][1] = $interest_row[0];
			$interest_result[$i][2] = $interest_row[1];
			$interest_result[$i][3] = $interest_row[2];
			$interest_result[$i][4] = $interest_row[3];
			$interest_result[$i][5] = $interest_row[4];
			$interest_result[$i][6] = $interest_row[5];
		}
		
		return $interest_data;		
	}

	function get_recent_product(){
		mysql_query("SET NAMES utf8");
		$sql = "SELECT * FROM product";
		$recent_result = mysql_query($sql);
		
		if(mysql_num_rows($recent_result) > 5){
			$recnet_count = 5;
		}
		else{
			$recnet_count = mysql_num_rows($recent_result);
		}
		for($i=1; $i<= $recnet_count; $i++){
			$recent_row = mysql_fetch_row($recent_result);
			$recent_data[$i][1] = $recent_row[1];
			$recent_data[$i][2] = $recent_row[2];
			$recent_data[$i][3] = $recent_row[3];
			$recent_data[$i][4] = $recent_row[4];
			$recent_data[$i][5] = $recent_row[5];
			$recent_data[$i][6] = $recent_row[6];
		}
		
		return $recent_data;

	}

	function get_best_product(){
		mysql_query("SET NAMES utf8");
		$sql = "SELECT * FROM product order by product_recommend DESC LIMIT 5";
		$best_result = mysql_query($sql);
		
		for($i=1; $i<=mysql_num_rows($best_result); $i++){
			$best_row = mysql_fetch_row($best_result);
			$best_data[$i][1] = $best_row[0];
			$best_data[$i][2] = $best_row[1];
			$best_data[$i][3] = $best_row[2];
			$best_data[$i][4] = $best_row[3];
			$best_data[$i][5] = $best_row[4];
			$best_data[$i][6] = $best_row[5];
		}
		return $best_data;
	}

	function get_product($product_name){
		mysql_query("SET NAMES utf8");
		$sql = "SELECT * FROM product WHERE product_name = '$product_name'";
		$get_product_result = mysql_query($sql);
		$get_row_count = mysql_fetch_row($get_product_result);
		return $get_row_count;
	}
	function add_product($post_user_code, $product_name, $product_content, $product_image, $manufacturer) {
		mysql_query("SET NAMES utf8");
		$sql = "INSERT INTO product (post_user_name,product_name,product_content, product_image, Manufacturer)VALUES ('$post_user_code', '$product_name', '$product_content', '$product_image', '$manufacturer');";
		$add_result = mysql_query($sql);
		
	}
	function get_product_name($compani_name) {
		mysql_query("SET NAMES utf8");
		$sql = "SELECT product_name FROM product WHERE Manufacturer = '$compani_name'";
		$products_names = mysql_query($sql);
		for($i=1; $i<=mysql_num_rows($products_names); $i++) {
			$products_names_row = mysql_fetch_row($products_names);
			$products_date[$i] = $products_names_row[0];
		}
		return $products_date;
	}
	
	
	function interest_product($interest, $user){
	mysql_query("SET NAMES utf8");
	$sql = "INSERT INTO user (facebook_usercode, interest_product)VALUES ('$user','$interest');";
	$add_result = mysql_query($sql);
	}
	
	function getInterestProduct($user){
	mysql_query("SET NAMES utf8");
	$sql = "SELECT * FROM user WHERE facebook_usercode = '$user'";
	$interest_result = mysql_query($sql);
	for($i=1; $i<=5; $i++) {
		$interest_data = mysql_fetch_row($interest_result);
		mysql_query("SET NAMES utf8");
		$sql_r = "SELECT product_name, product_content, product_image from product WHERE product_name = '$interest_data[2]'";
		$interest_product_result = mysql_query($sql_r);
		$interest_product_data = mysql_fetch_row($interest_product_result);
		$interestproduct[$i][1] = $interest_product_data[0];
		$interestproduct[$i][2] = $interest_product_data[1];
		$interestproduct[$i][3] = $interest_product_data[2];
	}
	
	return $interestproduct;
	}
	
?>
